package com.LeeCode;

/**
 * 二叉树中第二小的节点
 */

public class Code671 {
    public static void main(String[] args) {
        Integer[] arr = {1, 1, 3, 1, 1, 3, 4, 3, 1, 1, 8};
        TreeNode root = Utils.buildTree(arr);
        System.out.println(new Code671().findSecondMinimumValue(root));
    }

    public int findSecondMinimumValue(TreeNode root) {
        if (root.left == null) return -1;

        int left = root.left.val == root.val ? findSecondMinimumValue(root.left) : root.left.val;
        int right = root.right.val == root.val ? findSecondMinimumValue(root.right) : root.right.val;
        if (left == -1 && right == -1) {
            return -1;
        } else if (left == -1) {
            return right;
        } else if (right == -1) {
            return left;
        } else {
            return Math.min(left, right);
        }

    }
}
